package ciyuan.Block.Machine.Simple.Furnace;

import ciyuan.Block.Machine.Simple.SimpleMachineRegister;
import ciyuan.Block.TypeInterface.MachineTileEntity;
import ciyuan.Item.ItemRegisterManager;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;

/**
 * @author 150149
 */
public class SimpleFurnaceTileEntity extends MachineTileEntity {

    private static final String STONEPOT_OUT = "cystonepotout";
    private static final ITextComponent TITLE = new TranslationTextComponent("block.ciyuanrpg.simple_furnace");

    public SimpleFurnaceTileEntity() {
        super(SimpleMachineRegister.SIMPLE_FURNACE_TILE_ENTITY.get(), 4, TITLE, 800);
    }

    @Nullable
    @Override
    public Container createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
        return new SimpleFurnaceContainer(SimpleMachineRegister.SIMPLE_FURNACE_CONTAINER.get(), sycID, inventory, getBlockPos());
    }

    @Override
    public void tick() {
        if (!this.hasLevel() || this.level.isClientSide) {
            return;
        }
        ItemStack potItemStack = this.inventory.getItem(0);
        ItemStack inputItemStack = this.inventory.getItem(1);
        ItemStack fuelItemStack = this.inventory.getItem(2);

        if (potItemStack.isEmpty() || inputItemStack.isEmpty()) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (!potItemStack.getItem().equals(ItemRegisterManager.stonepot.get())) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (getOutputItem(inputItemStack.getItem()) == null) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (this.getProgress() >= this.getConsumeTime()) {
            ItemStack itemStack = new ItemStack(ItemRegisterManager.stonepot1.get());
            CompoundNBT compoundNBT = new CompoundNBT();
            compoundNBT.putString(STONEPOT_OUT, getOutputItem(inputItemStack.getItem()).getDescriptionId());
            itemStack.setTag(compoundNBT);
            if (this.inventory.canAddItem(itemStack)) {
                inputItemStack.setCount(this.inventory.getItem(0).getCount() - 1);
                potItemStack.setCount(this.inventory.getItem(1).getCount() - 1);
                this.resetProgress();
                this.inventory.addItem(itemStack);
            }
        } else if (!this.consumeFuel() && !fuelItemStack.isEmpty() && getFuelAddCount(fuelItemStack.getItem()) != 0) {
            fuelItemStack.setCount(fuelItemStack.getCount() - 1);
            this.addFuel(getFuelAddCount(fuelItemStack.getItem()));
            this.consumeFuel();
            this.setMachineStatus(true);
        }
    }

    private static Item getOutputItem(Item in) {
        if (in.equals(ItemRegisterManager.broken_aluminium.get())) {
            return ItemRegisterManager.Aluminium.get();
        } else if (in.equals(ItemRegisterManager.broken_copper.get())) {
            return ItemRegisterManager.Copper.get();
        } else if (in.equals(ItemRegisterManager.broken_gold.get())) {
            return Items.GOLD_INGOT;
        } else if (in.equals(ItemRegisterManager.broken_iron.get())) {
            return Items.IRON_INGOT;
        } else if (in.equals(ItemRegisterManager.broken_tin.get())) {
            return ItemRegisterManager.tin.get();
        }
        return null;
    }
}
